<?php  
class ControllerModuleArticleCategory extends Controller {
	protected function index($setting) {
		$this->language->load('module/article_category');
		if (isset($this->request->get['cpath'])) {
			$parts = explode('_', (string)$this->request->get['cpath']);
		} else {
			$parts = array();
		}
		
		if (isset($parts[0])) {
			$this->data['article_category_id'] = $parts[0];
		} else {
			$this->data['article_category_id'] = 0;
		}
		
		if (isset($parts[1])) {
			$this->data['child_id'] = $parts[1];
		} else {
			$this->data['child_id'] = 0;
		}
							
		$this->load->model('news/category');

		$this->load->model('news/article');
		$category_info = $this->model_news_category->getCategory($setting['parent_id']);
		if($setting['parent_id']==0){
		$this->data['heading_title'] = $this->language->get('heading_title');
		$this->data['href'] = FALSE;
		}else{		
		$this->data['heading_title'] = $category_info['name'];		
		$this->data['href'] = $this->url->link('news/category', 'cpath=' . $setting['parent_id']);
		}
		$this->data['categories'] = array();

		$categories = $this->model_news_category->getCategories($setting['parent_id']);

		foreach ($categories as $category) {
			$total = $this->model_news_article->getTotalArticles(array('filter_article_category_id'  => $category['article_category_id']));

			$children_data = array();

			$children = $this->model_news_category->getCategories($category['article_category_id']);

			foreach ($children as $child) {
				$data = array(
					'filter_article_category_id'  => $child['article_category_id'],
					'filter_sub_category' => true
				);

				$article_total = $this->model_news_article->getTotalArticles($data);

				$total += $article_total;

				$children_data[] = array(
					'article_category_id' => $child['article_category_id'],
					'name'        => $child['name'] . ($this->config->get('config_article_count') ? ' (' . $article_total . ')' : ''),
					'href'        => $this->url->link('news/category', 'cpath=' . $category['article_category_id'] . '_' . $child['article_category_id'])	
				);		
			}

			$this->data['categories'][] = array(
				'article_category_id' => $category['article_category_id'],
				'name'        => $category['name'] . ($this->config->get('config_article_count') ? ' (' . $total . ')' : ''),
				'children'    => $children_data,
				'href'        => $this->url->link('news/category', 'cpath=' . $category['article_category_id'])
			);	
		}
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/article_category.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/article_category.tpl';
		} else {
			$this->template = 'default/template/module/article_category.tpl';
		}
		
		$this->render();
  	}

}
?>